<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>我的地址</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<link rel="stylesheet" type="text/css" href="../fonts/iconfont/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../css/global.css" />
		<style type="text/css">
			.mui-bar-nav~.mui-content{padding-top: 0;}
			.mui-checkbox.mui-left input[type=checkbox],
			.mui-radio.mui-left input[type=radio] {
				left: 0;
			}
			.mui-table-view:nth-child(1){margin-top: 44px !important;}
			.mui-checkbox.mui-left label,
			.mui-radio.mui-left label {
				padding-left: 36px;
			}
			
			.mui-radio input[type=radio]:checked:before {
				content: '\e442'
			}
			.mui-content:first-child{margin-top: 44px;}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav bg_white">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title colorP">我的地址</h1>
			<button onclick="shop(0)" class="mui-btn-link mui-pull-right"><span class="colorP">新建</span></button>
		</header>
		<div class="mui-content" id='content'>
			
		</div>
	</body>
	<script type="text/html" id='pid'>
		<% for(var i in data){%>
		<ul class="mui-table-view margin-bottom10px bg_white">
			<li class="mui-table-view-cell mui-media">
				<a href="javascript:;">
					<span class="mui-pull-left iconfont color999 font26 padding_t10px">&#xe61c;</span>
					<div class="mui-media-body padding_l">
						<p class="color0 mui-clearfix line-height30px">
							<span class="mui-pull-left font18 uName"><%= data[i].name %></span>
							<span class="mui-pull-right font14 uTel"><%= data[i].mobile %></span>
						</p>
						<p class="color0 font14 uAddress">
							<%= data[i].address %>
						</p>
					</div>
				</a>
			</li>
			<li class="mui-table-view-cell mui-media padding_tb_0px">
				<div class="mui-input-row mui-radio mui-left mui-pull-left">
					<label>默认地址</label>
					<% if(data[i].state==1){%>
					<input name="radio" onclick="setdefault('<%=data[i].id%>','<%=data[i].uid%>')" type="radio" checked>
					<%  }else{ %>
					<input name="radio" onclick="setdefault('<%=data[i].id%>','<%=data[i].uid%>')" type="radio">
					<% }%>
				</div>
				<div class="mui-pull-right padding_tb11px">
					<span onclick="shop(<%=data[i].id %>)" class="mui-icon iconfont color999">&#xe62a;  编辑</span>
					<span class="mui-icon iconfont color999 padding_l del_btn" onclick="del('<%=data[i].id%>','<%=data[i].uid%>')">&#xe619;  删除</span>
				</div>
			</li>
		</ul>

		<% } %>
	</script>
	<script src="../js/mui.min.js"></script>
	<script src="../js/open.js"></script>
	<script src="../js/apppublic.js"></script>
	<script src="../js/jquery.min.js"></script>
	<script src="../js/template.js"></script>
	<script type="text/javascript">
		$('.del_btn').click(function() {
			$(this).parent().parent().parent().remove();
		})
		var userid,ws;
		document.addEventListener('plusready', function(){
			userid = plus.storage.getItem('uid');
			ws=plus.webview.currentWebview();
			initveiw();
		}, false)
        function shop(id){
        	openPage('create_address.html',{aid:id,w:ws.id})
        }
		function setdefault(id, uid){
			$.ajax({
				type: 'post',
				url: apiRoot,
				data: { action: "Address.action", type: 'setdefault', uid: uid, id: id },
				dataTpye: 'json',
				success: function(data) {
					if(data.error == 0) {
						initveiw();
					}
					console.log(JSON.stringify(data))
				},
				error: function(e) {
					console.log(JSON.stringify(e))
				}
			})
		}

		function initveiw() {
			$.ajax({
				type: 'post',
				url: apiRoot,
				data: { action: "Address.allAddress", uid: userid },
				dataTpye: 'json',
				success: function(data) {
					var html = template.render('pid', data);
					$("#content").html(html);
				},
				error: function(e) {

				}
			})
		}

		function del(id, uid) {
			$.ajax({
				type: 'post',
				url: apiRoot,
				data: { action: "Address.action", type: 'del', uid: uid, id: id },
				dataTpye: 'json',
				success: function(data) {
					if(data.error == 0) {
						initveiw();
					}
					console.log(JSON.stringify(data))
				},
				error: function(e) {
					console.log(JSON.stringify(e))
				}
			})
		}
		
//		var parentid = plus.webview.getWebviewById('confirm_order.html');
//     	mui.fire(parentid,'refresh_address',{u_name:$('.uName').html(),u_phone:$('.uTel').html(),u_address:$('.uAddress').html()});
	</script>
</html>